package cn.swing.his.finance.webapi.service.impl;

import cn.swing.his.common.exception.HisServiceException;
import cn.swing.his.common.web.JsonPage;
import cn.swing.his.common.web.ResponseCode;
import cn.swing.his.finance.service.IFinanceDetailService;
import cn.swing.his.finance.webapi.mapper.FinanceDetailMapper;
import cn.swing.his.pojo.finance.dto.AddFinanceDetailDTO;
import cn.swing.his.pojo.finance.dto.TimeDTO;
import cn.swing.his.pojo.finance.entity.FinanceDetail;
import cn.swing.his.pojo.finance.vo.FinanceDetailVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
@DubboService
public class FinanceDetailServiceImpl implements IFinanceDetailService {
    @Autowired
    private FinanceDetailMapper financeDetailMapper;

    @Override
    public JsonPage<FinanceDetailVO> financeDetailList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<FinanceDetailVO> list =financeDetailMapper.financeDetailList();
        return JsonPage.restPage(new PageInfo<>(list));
    }

    @Override
    public void insertFinanceDetail(AddFinanceDetailDTO addFinanceDetailDTO) {
        FinanceDetail financeDetail=new FinanceDetail();
        BeanUtils.copyProperties(addFinanceDetailDTO,financeDetail);
        LocalDateTime now=LocalDateTime.now();
        financeDetail.setDealTime(now);
        int rows=financeDetailMapper.insertFinanceDetail(financeDetail);
        if (rows==0){
            /*插入失败*/
            throw new HisServiceException(ResponseCode.BAD_REQUEST,"服务器异常");
        }
    }

//    @Override
//    public List<FinanceDetail> financeDetailAllList() {
//        List<FinanceDetail> list=financeDetailMapper.financeDetailAllList();
//        for (FinanceDetail f:list ) {
//            f.setType("0".equals(f.getType())?"收入":"支出");
//        }
//        return list;
//    }

    @Override
    public List<FinanceDetail> financeDetailAllList(TimeDTO time) {
        List<FinanceDetail> list=financeDetailMapper.financeDetailAllLists(time.getStartDate(),time.getEndDate());
        for (FinanceDetail f:list ) {
            f.setType("0".equals(f.getType())?"收入":"支出");
        }
        return list;
    }

    @Override
    public JsonPage<FinanceDetail> getFinanceDetailByPage(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<FinanceDetail> list=financeDetailMapper.financeDetailAllList();
        return JsonPage.restPage(new PageInfo<>(list));
    }
}
